//
//  AKImageKit.m
//  AKImageToolKit
//
//  Created by Chen Ben on 11/15/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "AKImageKit.h"


@implementation AKImageKit

- (unsigned int)loadImagesFromDirectory:(NSString*)dirPath 
							   withType:(NSString*)imageType
{
	// Obtain the default file manager
	NSFileManager*	fileManager = [NSFileManager defaultManager];
	
	// Check if the path exist and is a directory
	BOOL isDir;
	if (NO == [fileManager fileExistsAtPath:dirPath 
								isDirectory:&isDir]) 
	{
		// Raise a exception...
		return 0;
	} else if (NO == isDir) {
		// Raise a exception...
		return 0;
	}
	
	// Load all image with specific type in the directory into the imagesInDirectory
	NSDirectoryEnumerator *dirEnum =
		[fileManager enumeratorAtPath:dirPath];
	
	NSMutableArray* tempImages = [NSMutableArray array];
	NSString *fileName;

	while (fileName = [dirEnum nextObject]) {
		// Skip all sub-directories
		[dirEnum skipDescendents];
		if ([[fileName pathExtension] isEqualToString: imageType]) {
			[tempImages addObject:[[NSImage alloc] initWithContentsOfFile:
				[dirPath stringByAppendingPathComponent:fileName]]];
		}
	}

	[imagesInDirectory autorelease];
	imagesInDirectory = [tempImages copy];
	return [imagesInDirectory count];
}

- (BOOL)combineImagesToNewCombination
{
	if (!imagesInDirectory || 0 == [imagesInDirectory count]) {
		return NO;
	}
	
	// Get the size of the bigImage
	NSImage* sampleImage = [imagesInDirectory objectAtIndex:0];
	NSSize	 imageSize = [sampleImage size];
	unsigned imageNumber = [imagesInDirectory count];
	
	NSRect bigImageRect;
	bigImageRect.origin = NSZeroPoint;
	bigImageRect.size = NSMakeSize(imageSize.width*imageNumber, imageSize.height);
	
	// Draw all to the newCombination
	[newCombination release];
	newCombination = [[NSImage alloc] initWithSize:bigImageRect.size];
	
	[newCombination lockFocus];
	
	unsigned int index;
	NSRect	drawingRect = NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height);
	NSRect	imageRect   = drawingRect;
	for(index = 0; index < imageNumber; ++index)
	{
		NSImage*	anImage = [imagesInDirectory objectAtIndex:index];
		[anImage drawInRect:drawingRect 
				   fromRect:imageRect 
				  operation:NSCompositeSourceOver 
				   fraction:1.0];
		drawingRect.origin.x += imageSize.width;
	}
	
	[newCombination unlockFocus];
	
	return YES;
}

- (BOOL)drawNewCombinationToCurrentImageWithOperation:(NSCompositingOperation)aOperation
											 fraction:(float)aFraction
{
	if (!newCombination) {
		return NO;
	}
	
	// Create a empty image if currentImage does not exsit
	if (!currentImage) {
		if (aOperation == NSCompositeSourceOver && aFraction >= 1.0) {
			currentImage = [newCombination copy];
			return YES;
		} else {
			currentImage = [[NSImage alloc] initWithSize:[newCombination size]];
		}
	}
	
	// Draw the newCombination to currentImage
	[currentImage lockFocus];
	
	NSRect	drawingRect;
	drawingRect.origin = NSZeroPoint;
	drawingRect.size = [newCombination size];
	
	NSRect	imageRect   = drawingRect;
	
	[newCombination drawInRect:drawingRect
					  fromRect:imageRect
					 operation:aOperation
					  fraction:aFraction];
	
	[currentImage unlockFocus];
	
	return YES;
}

- (BOOL)saveCurrentImageToPath:(NSString*)filePath
{
	if (!currentImage) {
		return NO;
	}
	
	return [NSKeyedArchiver archiveRootObject:currentImage toFile:filePath];
}

- (NSImage*)currentImage
{
	return currentImage;
}

- (void)setCurrentImage:(NSImage*)aImage
{
	[currentImage autorelease];
	currentImage = [aImage retain];
}

- (NSImage*)combineImage:(NSImage*)image1 
			   withImage:(NSImage*)image2 
			   operation:(NSCompositingOperation)aOperation
				fraction:(float)aFraction
{
	NSRect drawingRect;
	drawingRect.origin = NSZeroPoint;
	drawingRect.size = [image2 size];
	NSRect imageRect = drawingRect;
	
	[image1 lockFocus];
	
	[image2 drawInRect:drawingRect
			  fromRect:imageRect
			 operation:aOperation 
			  fraction:aFraction];
	
	[image1 unlockFocus];
	
	return image1;
}
- (void) dealloc {
	[currentImage release];
	[imagesInDirectory release];
	[newCombination release];
	[super dealloc];
}

@end
